﻿using System.Configuration;
using System.Linq;
using System.Windows;

namespace WPFMultipleLanguage
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        //更换语言
        public static void UpdateLanguage(string lan)
        {
            // 获取配置
            string requestedLanguage = $"{lan}.xaml";
            ResourceDictionary resourceDictionary = Application.Current.Resources.MergedDictionaries.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedLanguage));
            Current.Resources.MergedDictionaries.Remove(resourceDictionary);
            Current.Resources.MergedDictionaries.Add(resourceDictionary);

            // 保存配置
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConfigurationManager.AppSettings["Language"] = lan;
            config.Save(ConfigurationSaveMode.Modified);
            //刷新
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
